//
//  YLSheetView.m
//  YLProject
//
//  Created by 张伯林 on 2018/2/3.
//  Copyright © 2018年 WY. All rights reserved.
//

#import "YLSheetView.h"
#import "YLSheetViewCell.h"
@interface YLSheetView ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIView *backView;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *dataSource;
@end

@implementation YLSheetView
+(instancetype)initWithDataSource:(NSArray *)dataSource{
    YLSheetView *sheetView = [[YLSheetView alloc]init];
    sheetView.dataSource = dataSource;
    return sheetView;
}
- (instancetype)init {
    if (self = [super init]) {
        self.modalPresentationStyle = UIModalPresentationCustom;
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.backView];
    [self createView];
}
-(void)createView{
    switch (self.type) {
        case YLSheetViewReportLaBlack:
        {
            [self.view addSubview:self.tableView];
        }
            break;
            
        default:
            break;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 49 * YL_KEY;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YLSheetViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(YLSheetViewCell.class)];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    cell.sheetViewTitleLabel.text = self.dataSource[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.dataSource.count - 1) {
        [self dismissSheetView];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(sheetView:currentSheetViewType:didSelectedIndex:)]) {
        [self.delegate sheetView:self currentSheetViewType:self.type didSelectedIndex:indexPath.row];
    }
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,Screen_Height - self.dataSource.count * 49 * YL_KEY - YLSafeAreaBottomHeight, Screen_Width, self.dataSource.count * 49 * YL_KEY) style:UITableViewStylePlain];
        _tableView.bounces = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
//        [_tableView registerClass:UITableViewCell.class forCellReuseIdentifier:NSStringFromClass(UITableViewCell.class)];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass(YLSheetViewCell.class) bundle:nil] forCellReuseIdentifier:NSStringFromClass(YLSheetViewCell.class)];
    }
    return _tableView;
}
-(UIView *)backView{
    if (!_backView) {
        _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height - YLSafeAreaBottomHeight)];
        _backView.backgroundColor = [UIColor blackColor];
        _backView.alpha = 0.5;
    }
    return _backView;
}
-(void)showSheetView:(UIViewController *)controller{
    [controller presentViewController:self animated:NO completion:nil];
}
-(void)dismissSheetView{
    [self dismissViewControllerAnimated:NO completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
